package io.sage.modules.socket.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import io.sage.common.utils.Change;
import io.sage.modules.socket.service.SocketClientWeatherService;
import io.sage.modules.statistics.service.BStatisticsSoilWaterService;
import io.sage.modules.statistics.service.WeatherService;

/**
 * Created by Mafy on 2019/7/15.
 */
@Service("socketClientWeatherServiceImpl")
public class SocketClientWeatherServiceImpl extends Thread implements SocketClientWeatherService{
	
	@Autowired
	private WeatherService weatherService;
	
	@Autowired
	private BStatisticsSoilWaterService statisticsSoilWaterService;

    //定义一个Socket对象
    Socket socket = null;
    //写操作
    OutputStream os= null;

    @Value(value="${socket.serverIp1}")
    private String host;

    @Value(value="${socket.serverPort1}")
    private int port;

    private synchronized String init() {
        try {
            //需要服务器的IP地址和端口号，才能获得正确的Socket对象
            socket = new Socket(host,port);
            os= socket.getOutputStream();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("连接服务器失败");
            e.printStackTrace();
        }
        return "success";
    }

    /**
     * 读取服务端返回的数据
     * 现在没用，如果发送数据是原路返回响应值，则使用，现在用socketserver里面的线程处理返回的数据
     */
    @Override
    public void run() {
        //客户端一连接就可以写数据个服务器了
        try {
            // 读Sock里面的数据
            if(ObjectUtils.isEmpty(socket)){
                init();
            }
            InputStream s = socket.getInputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = s.read(buf)) != -1) {
                System.out.println("引用websocket返回頁面數據");
                String ind = "000000000000000000000000000000000000000";
                String buff = Change.byteArrayToHexString(buf);
                System.out.println(buff.split(ind)[0]);
                String string = buff.split(ind)[0];
                //实时天气
                
                if(string.length()==62){
                	if("FF031A".equals(buff.split(ind)[0].substring(0, 6))){
                		System.out.println("保存天气数据:"+buff.split(ind)[0]);
                		weatherService.insert(buff.split(ind)[0]);
                	}
                }
                
                if("070302".equals(buff.split(ind)[0].substring(0, 6))){
                		System.out.println("保存土壤水分数据:"+buff.split(ind)[0]);
                		statisticsSoilWaterService.insert(buff.split(ind)[0]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String sendMsg(String msg){
        try {
            if(ObjectUtils.isEmpty(socket)){
                init();
            }
            os.write(Change.HexString2Bytes(msg));
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
